﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using bll;
using System.Threading;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;

namespace TimeIt.beispiele
{
    public partial class employee : System.Web.UI.Page 
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            bll.clsUserCollection _UserCol = new bll.clsUserCollection();

            bll.clsUser _clsIdUser = new bll.clsUser();
            Int32 sessionId = bll.clsUser.Session;
            if (sessionId == 0) { Response.Redirect("login.aspx"); }
            _clsIdUser = _UserCol.GetUserNameBySessionId();
            UserName.Text = _clsIdUser.Title + " " + _clsIdUser.Name;
            //AutoLogout myThread = new AutoLogout();
            //startSession();
        }

        /// <summary>
        /// By click on the button the Timesheet will be added to the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
                txtDate.Text = "";
                foreach (DateTime day in Calendar1.SelectedDates)
                {
                    txtDate.Text += day.Date.ToShortDateString();
                }
        }//Calendar1_SelectionChanged



        /// <summary>
        /// By click on the button the Timesheet will be added to the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btninsert_Click(object sender, System.EventArgs e)
        {
            //Delivers the value to the bll!
            String[] msg_Array = new String[5];
            functionAdd add = new functionAdd();
            String[] msgFilled = add.addTimeSheet(Convert.ToInt32(DropDownProject.Text), 
                                                 txtDate.Text,
                                                 txtHours.Text, 
                                                 txtDescription.Text, 
                                                 checkChargeable.Checked,
                                                 DropDownCategory.Text);
            
            dateError.Text = msg_Array[2];
            txtHoursError.Text = msg_Array[3];
            txtDescriptionError.Text = msg_Array[4];

            //Analyze the result
            if (msgFilled[1] == "success")
            {
                //Add Message
                checkMsg.ForeColor = System.Drawing.Color.Green;
                checkMsg.Text = "Adding the working time (hours: " + Convert.ToInt32(txtHours.Text) + " date: " +
                    Convert.ToDateTime(txtDate.Text).ToString("yyyy.MM.dd", System.Globalization.CultureInfo.InvariantCulture) +
                                ") successfully for the Project " + DropDownProject.DataTextField + "!";

                //Delete warnings of fields (e. g. in case of page reload)
                txtDate.Text = ""; txtDescription.Text = ""; txtHours.Text = "";
            }
            else if(msgFilled[0] != "fail")
            {
                //Add Message(Warning)
                checkMsg.ForeColor = System.Drawing.Color.Red;
                checkMsg.Text = "Error! Adding the working time (hours: " + Convert.ToInt32(txtHours.Text) + " date: " + 
                    Convert.ToDateTime(txtDate.Text).ToString("yyyy.MM.dd", System.Globalization.CultureInfo.InvariantCulture) +
                                ") failed for the Project " + DropDownProject.DataTextField + "!";
            }
            //Update of the TimeSheet
            grdTimeSheet.DataBind();

        }//btninsert_Click

        
        //Starts a new thread in the Background
        protected void startSession()
        {
            ThreadStart ths = new ThreadStart(sessionZero);
            Thread th = new Thread(ths);

            // Ensures background thread is killed when last foreground terminates
            th.IsBackground = true;

            // Don't want this thread taking up too much CPU            
            th.Priority = ThreadPriority.BelowNormal;
            th.Start();
        }//startSession

        //This Method checks every 5 seconds the sessionID and if its not actual 
        //the system logs out the user automatically in the System.
        protected void sessionZero()
        {
            while (true)
            {
                Int32 _session = bll.clsUser.Session;
                if (_session == 0)
                {
                    MessageBox.Show("Timeout. Please login again!");
                    new employee();
                    break;
                }
                Thread.Sleep(5000);
            }
        }
    }
}